/**
 * 使用普通的数据操作
 */
package aorey.database;


import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.dom4j.DocumentException;
import aorey.config.FrameworkConfig;
import aorey.database.config.DataConfigDelete;
import aorey.database.config.DataConfigInsert;
import aorey.database.config.DataConfigSelect;
import aorey.database.config.DataConfigUpdate;
import aorey.nosql.EhcacheUtil;

public class DataConfig {

	private HttpServletRequest request;
	private EhcacheUtil framework;
	public DataConfig(HttpServletRequest request)
	{
		this.request = request;
		this.framework = (EhcacheUtil)request.getServletContext().getAttribute("aorey");	
	}
	
	// 从内存或缓存中获取配置

	@SuppressWarnings("deprecation")
	public DataConfigSelect getSelect(String id) throws Exception
	{
		DataConfigSelect dataConfigSelect = (DataConfigSelect)this.framework.get("select", id) ;
		
		// 从文件加载配置
		if (dataConfigSelect == null) {	
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append(this.request.getRealPath(""));
			stringBuilder.append(FrameworkConfig.selectConfig);
			stringBuilder.append("/");
			stringBuilder.append(id);
			stringBuilder.append(".xml");
			File config = new File(stringBuilder.toString());
			if (config.exists()) {
				dataConfigSelect = new DataConfigSelect(stringBuilder.toString(),this.request);
				System.out.println("write cache.");
				this.framework.put("select", id, dataConfigSelect);
			}
			else
			{
				System.out.println("Select 配置文件不存在.");
				throw new Exception("Select config file is not found.");
			}
		}
		return dataConfigSelect;
	}
	
	@SuppressWarnings("deprecation")
	public DataConfigInsert getInsert(String id) throws DocumentException
	{
		DataConfigInsert dataConfigInsert = (DataConfigInsert) this.framework.get("insert", id) ;		
		if (dataConfigInsert == null) {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append(this.request.getRealPath(""));
			stringBuilder.append(FrameworkConfig.insertConfig);
			stringBuilder.append("/");
			stringBuilder.append(id);
			stringBuilder.append(".xml");
			File config = new File(stringBuilder.toString());
			if (config.exists()) {
				dataConfigInsert = new DataConfigInsert(stringBuilder.toString());
				this.framework.put("insert", id, dataConfigInsert);
			}
			else
			{
				System.out.println("Insert 配置文件不存在!");
			}
		}
		return dataConfigInsert;		
	}
	
	@SuppressWarnings("deprecation")
	public DataConfigUpdate getUpdate(String id) throws DocumentException
	{
		DataConfigUpdate dataConfigUpdate = (DataConfigUpdate) this.framework.get("update", id) ;
		if (dataConfigUpdate==null) {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append(this.request.getRealPath(""));
			stringBuilder.append(FrameworkConfig.updateConfig);
			stringBuilder.append("/");
			stringBuilder.append(id);
			stringBuilder.append(".xml");
			File config = new File(stringBuilder.toString());
			if (config.exists()) {
				dataConfigUpdate = new DataConfigUpdate(stringBuilder.toString(),request);
			}
			else
			{
				System.out.println("Update 配置文件不存在.");
			}
		}
		return dataConfigUpdate;
	}
	
	@SuppressWarnings("deprecation")
	public DataConfigDelete getDelete(String id) throws Exception
	{
		DataConfigDelete dataConfigDelete = (DataConfigDelete) this.framework.get("delete", id) ;
		if (dataConfigDelete == null) {
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append(this.request.getRealPath(""));
			stringBuilder.append(FrameworkConfig.deleteConfig);
			stringBuilder.append("/");
			stringBuilder.append(id);
			stringBuilder.append(".xml");
			File config = new File(stringBuilder.toString());
			if (config.exists()) {
				dataConfigDelete = new DataConfigDelete(stringBuilder.toString(),this.request);
				this.framework.put("delete", id, dataConfigDelete);
			}
			else
			{
				System.out.println("Delete 配置文件不存在.");
				throw new Exception("Delete config file is not found.");
			}
		}
		return dataConfigDelete;
	}
}
